﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YSC_Core.BLL.Domain.CommonModels;
using YSC_Core.BLL.Domain.Entity;
using YSC_Core.BLL.Domain.Utils;
using YSC_Core.BLL.Domain.ViewModels;
using YSC_Core.BLL.Repository;
using YSC_OpenPlatform_API.Entity;
using YSC_Services.BLL.Services.Interfaces;

namespace YSC_Services.BLL.Services.Implementations
{
    /// <summary>
    /// 首页接口实现
    /// </summary>
    public class HomeAppServices : IHomeAppServices
    {
        private IEfRepository _rep;

        public HomeAppServices(IEfRepository rep)
        {
            this._rep = rep;
        }



        /// <summary>
        /// 首页活动
        /// </summary>
        /// <returns></returns>
        public ReturnValueModel GetHomeActivityAll()
        {
            var activityAll = from a in _rep.Where<NewsArticle>(p => p.Status == 1 && p.Type == "活动")
                              join m in _rep.Where<MediaImage>(p => true) on a.Id equals m.BllId
                              select new
                              {
                                  a.Id,
                                  a.Title,
                                  m.FileUrl,
                                  a.CreateTime
                              };

            var models = activityAll.ToList();
            if (models.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到活动信息!" };

            return new ReturnValueModel { Code = 200, Msg = "success", Result = models };
        }

        /// <summary>
        /// 活动详情
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        public ReturnValueModel GetHomeActivity(string activityId)
        {
            if (string.IsNullOrEmpty(activityId))
                return new ReturnValueModel { Code = 400, Msg = "活动主键Does Not Exist!" };


            var activity = _rep.Where<NewsArticle>(p => p.Status == 1 && p.Type == "活动" && p.Id == activityId).FirstOrDefault();
            if (activity != null)
            {
                activity.IntRead = activity.IntRead + 1;
                _rep.Update(activity);
            }


            var item = _rep.Where<NewsArticle>(p => p.Status == 1 && p.Type == "活动" && p.Id == activityId)
                .Select(p => new
                {
                    p.Id,
                    p.Author,
                    p.Title,
                    p.Details,
                    p.CreateTime
                });

            var models = item.ToList();
            if (models.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到活动信息!" };

            return new ReturnValueModel { Code = 200, Msg = "success", Result = models.FirstOrDefault() };
        }

        /// <summary>
        /// 首页文章列表
        /// </summary>
        /// <param name="intType">0:热门推荐 1:最新资讯</param>
        /// <returns></returns>
        public ReturnValueModel GetHomeNewsArticleAll(int intType)
        {
            var newsArticleAll = from a in _rep.Where<NewsArticle>(p => p.Status == 1 && p.Type == "新闻动态")
                                 join m in _rep.Where<MediaImage>(p => true) on a.Id equals m.BllId
                                 select new
                                 {
                                     a.Author,
                                     a.Title,
                                     a.IntRead,
                                     a.CreateTime,
                                     FishUrl = string.IsNullOrEmpty(m.FileUrl) ? m.FileUrl : string.Format("https://fishopeni.cnfm.com.cn:8095" + m.FileUrl)
                                 };

            var newsArticleList = newsArticleAll.Take(4);
            if (intType == 0)
            {
                var models = newsArticleList.OrderByDescending(p => p.IntRead).ToList();
                return new ReturnValueModel { Code = 200, Msg = "success", Result = models };
            }

            var model = newsArticleList.OrderByDescending(p => p.CreateTime).ToList();
            return new ReturnValueModel { Code = 200, Msg = "success", Result = model };

        }

        /// <summary>
        /// 文章详情
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public ReturnValueModel GetHomeNewsArticle(string newsId)
        {
            if (string.IsNullOrEmpty(newsId))
                return new ReturnValueModel { Code = 400, Msg = "资讯主键Does Not Exist!" };


            var activity = _rep.Where<NewsArticle>(p => p.Status == 1 && p.Type == "新闻动态" && p.Id == newsId).FirstOrDefault();
            if (activity != null)
            {
                activity.IntRead = activity.IntRead + 1;
                _rep.Update(activity);
            }


            var item = _rep.Where<NewsArticle>(p => p.Status == 1 && p.Type == "新闻动态" && p.Id == newsId)
                .Select(p => new
                {
                    p.Id,
                    p.Author,
                    p.Title,
                    p.Details,
                    p.CreateTime
                });

            var models = item.ToList();
            if (models.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到资讯信息!" };

            return new ReturnValueModel { Code = 200, Msg = "success", Result = models.FirstOrDefault() };
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="sasearch">模糊条件:作者 标题</param>
        /// <param name="intType">0:热门推荐 1:最新资讯</param>
        /// <returns></returns>
        public ReturnValueModel GetNewsArticleAll(int index, int size, string sasearch, int intType)
        {
            var newsArticleAll = from a in _rep.Where<NewsArticle>(p => p.Status == 1 && p.Type == "新闻动态")
                                 join m in _rep.Where<MediaImage>(p => true) on a.Id equals m.BllId
                                 select new
                                 {
                                     a.Author,
                                     a.Title,
                                     a.IntRead,
                                     a.CreateTime,
                                     FishUrl = string.IsNullOrEmpty(m.FileUrl) ? m.FileUrl : string.Format("https://fishopeni.cnfm.com.cn:8095" + m.FileUrl)
                                 };

            var newsArticleList = newsArticleAll
                 .WhereIf(p => p.Author.Contains(sasearch) || p.Title.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false);

            if (intType == 0)
            {
                var models = newsArticleAll.OrderByDescending(p => p.IntRead).ToPaginationList(index, size).ToList();
                return new ReturnValueModel { Code = 200, Msg = "success", Result = models };
            }

            var model = newsArticleAll.OrderByDescending(p => p.CreateTime).ToPaginationList(index, size).ToList();
            return new ReturnValueModel { Code = 200, Msg = "success", Result = model };
        }

        /// <summary>
        /// 首页实时价格
        /// </summary>
        /// <param name="areaName"></param>
        /// <returns>Gain:0平价，1.涨幅 2.降低</returns>
        public ReturnValueModel GetHomeRealTimeAll(string areaName)
        {

            var sql = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
            var areaAll = _rep.SqlQuery<Area>(sql, string.IsNullOrEmpty(areaName) ? "上海市" : areaName).Select(p => new Area { YunCode = p.YunCode }).ToList();

            List<string> arrt = new List<string>();
            foreach (var item in areaAll)
                arrt.Add(item.YunCode);
            string[] arrt2 = arrt.ToArray();

            var marktList = from a in _rep.Where<MarketPrice>(p => p.Status == 0)
                            where a.CreateTime >= DateTime.Now.AddDays(-30) && a.Remarks != "App直报"
                            join f in _rep.Where<FishSpecies>(p => true) on a.FishId equals f.FishCode
                            join m in _rep.Where<MarketUnit>(p => true) on a.MarketId equals m.Id
                            join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                            where string.IsNullOrEmpty(areaName) ? a.Status == 0 : arrt2.Contains(r.YunCode)
                            select new
                            {
                                a.Id,
                                a.UserId,
                                a.MarketId,
                                a.FishId,
                                f.FishName,
                                m.MarketName,
                                a.AveragePrice,
                                a.CreateTime
                            };

            //var sql1 = marktList
            //    .OrderByDescending(p => p.CreateTime)
            //     .ToPaginationList(1, 2).ToSql();

            var marktPriceAll = marktList
                .OrderByDescending(p => p.CreateTime)
                 .ToPaginationList(1, 2).ToList()
                 .Join(_rep.Where<YscUser>(p => p.Status == 0), a => a.UserId, y => y.Id, (a, y) => new
                 { a.Id, a.FishName, y.UserName, a.UserId, a.AveragePrice, a.CreateTime, a.FishId, a.MarketName }).ToList()
                 .Select(p => new HomeTimePrintModel
                 {
                     Id = p.Id,
                     userId = p.UserId,
                     FishId = p.FishId.Trim(),
                     FishName = p.FishName,
                     UserName = p.UserName,
                     Print = p.AveragePrice,
                     TypeName = "市场价"
                 });


            foreach (var item in marktPriceAll)
            {
                var yesterdayPrint = _rep.FirstOrDefault<MarketPrice>(p => p.UserId == item.userId && p.FishId.Trim() == item.FishId.Trim() && p.CreateTime < DateTime.Now);
                if (yesterdayPrint == null)
                {
                    item.Gain = 0;
                    continue;
                }
                if (yesterdayPrint.AveragePrice > item.Print)
                    item.Gain = 2;
                else if (yesterdayPrint.AveragePrice < item.Print)
                    item.Gain = 1;
                else
                    item.Gain = 0;
            }

            var placeList = from a in _rep.Where<PlaceOfOriginPrice>(p => p.Status == 0)
                            where a.Remarks != "App直报"
                            join f in _rep.Where<FishSpecies>(p => true) on a.FishId equals f.FishCode
                            join m in _rep.Where<PlaceOfOriginUnit>(p => true) on a.PlaceOfOriginId equals m.Id
                            join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                            where string.IsNullOrEmpty(areaName) ? a.Status == 0 : arrt2.Contains(r.YunCode)
                            select new
                            {
                                a.Id,
                                a.UserId,
                                a.PlaceOfOriginId,
                                a.FishId,
                                f.FishName,
                                m.PlaceOriginName,
                                a.AveragePrice,
                                a.CreateTime
                            };

            var placePriceAll = placeList
                .OrderByDescending(p => p.CreateTime)
                 .ToPaginationList(1, 2).ToList()
                 .Join(_rep.Where<YscUser>(p => p.Status == 0), a => a.UserId, y => y.Id, (a, y) => new
                 { a.Id, a.FishName, y.UserName, a.UserId, a.AveragePrice, a.CreateTime, a.FishId, a.PlaceOriginName }).ToList()
                 .Select(p => new HomeTimePrintModel
                 {
                     Id = p.Id,
                     userId = p.UserId,
                     FishId = p.FishId.Trim(),
                     FishName = p.FishName,
                     UserName = p.UserName,
                     Print = p.AveragePrice,
                     TypeName = "产地价"
                 });

            foreach (var item in placePriceAll)
            {
                var yesterdayPrint = _rep.FirstOrDefault<PlaceOfOriginPrice>(p => p.UserId == item.userId && p.FishId.Trim() == item.FishId.Trim() && p.CreateTime < DateTime.Now);
                if (yesterdayPrint == null)
                {
                    item.Gain = 0;
                    continue;
                }
                if (yesterdayPrint.AveragePrice > item.Print)
                    item.Gain = 2;
                else if (yesterdayPrint.AveragePrice < item.Print)
                    item.Gain = 1;
                else
                    item.Gain = 0;
            }

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { MarktPriceAll = marktPriceAll, PlacePriceAll = placePriceAll } };
        }

        /// <summary>
        /// 市场实时渔家列表
        /// </summary>
        /// <param name="areaName"></param>
        /// <returns>Gain:0平价，1.涨幅 2.降低</returns>
        public ReturnValueModel GetMarketTimeAll(int index, int size, string areaName, string areaId, int priceType)
        {

            var fingerAll = _rep.Where<FishSpecies>(p => p.Status == 0).ToList();

            if (priceType == 0)
            {
                var sqlM = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                var areaAllM = _rep.SqlQuery<Area>(sqlM, string.IsNullOrEmpty(areaName) ? "上海市" : areaName).Select(p => new Area { YunCode = p.YunCode }).ToList();

                List<string> arrtM = new List<string>();
                foreach (var item in areaAllM)
                    arrtM.Add(item.YunCode);
                string[] arrt2M = arrtM.ToArray();

                var marketUnitList = from m in _rep.Where<MarketUnit>(p => p.Status == 0)
                                     join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                     where !string.IsNullOrEmpty(areaId) ? r.YunCode == areaId : arrt2M.Contains(r.YunCode)
                                     select new
                                     { m.Id };

                List<string> marketUnit = new List<string>();
                foreach (var item in marketUnitList.ToList())
                    marketUnit.Add(item.Id);

                string[] marketUnits = marketUnit.ToArray();


                var MarketPriceAll = from a in _rep.Where<MarketPrice>(p => p.Status == 0)
                                     where a.CreateTime >= DateTime.Now.AddDays(-15) && marketUnits.Contains(a.MarketId)
                                     join m in _rep.Where<MarketUnit>(p => p.Status == 0) on a.MarketId equals m.Id
                                     //join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                     //where !string.IsNullOrEmpty(areaId) ? r.YunCode == areaId : arrt2M.Contains(r.YunCode)
                                     // join y in _rep.Where<YscUser>(p => p.Status == 0) on a.UserId equals y.Id
                                     join f in _rep.Where<FishSpecies>(p => p.Status == 0) on a.FishId equals f.FishCode
                                     join d in _rep.Where<Dictionary>(p => p.Status == 0) on f.Type equals d.Id
                                     select new
                                     {
                                         a.Id,
                                         f.FishName,
                                         fishType = d.Name,
                                         // y.UserName,
                                         a.UserId,
                                         m.Address,
                                         a.AveragePrice,
                                         a.CreateTime
                                     };


                //var sql1 = MarketPriceAll.OrderByDescending(p => p.CreateTime)
                //     .ToPaginationList(index, size).ToSql();


                var modelM = MarketPriceAll.OrderByDescending(p => p.CreateTime)
                        .ToPaginationList(index, size)
                        .ToList().Join(_rep.Where<YscUser>(p => p.Status == 0), a => a.UserId, y => y.Id, (a, y) => new { a.Id, a.FishName, y.UserName, a.Address, a.AveragePrice, a.CreateTime, a.UserId }).ToList();





                List<string> arrtIdsM = new List<string>();
                foreach (var item in modelM)
                    arrtIdsM.Add(item.Id);
                string[] arrtIdM = arrtIdsM.ToArray();

                var marketPricesAllM = from a in _rep.Where<MarketPrices>(p => p.Status == 0)
                                       where a.CreateTime >= DateTime.Now.AddDays(-15) && arrtIdM.Contains(a.MarketPriceId)
                                       join f in _rep.Where<FishSpecies>(s => s.Status == 0) on a.DescriptionId.Trim() equals f.FishCode
                                       select new MarketPricesModel
                                       {
                                           DescriptionId = a.DescriptionId,
                                           MarketPriceId = a.MarketPriceId,
                                           Description = f.Description.Trim(),
                                           Price = a.Price
                                       };

                var marketPricessqlM = marketPricesAllM.ToSql();

                var marketPricesListM = marketPricesAllM.ToList();



                var modelsM = modelM
                .Select(p => new MarketPriceModel
                {
                    Id = p.Id,
                    FishName = p.FishName,
                    UserId = p.UserId,
                    UserName = p.UserName,
                    AveragePrice = p.AveragePrice,
                    UnitAddress = p.Address,
                    CreateTime = p.CreateTime,
                    MarketPricesModel = marketPricesListM.Where(a => a.MarketPriceId == p.Id).DistinctBy(a => new { Description = a.Description.Trim() }).ToList()
                }).ToList();


                foreach (var item in modelsM)
                {
                    foreach (var items in item.MarketPricesModel)
                    {
                        var yesterdayPrint = _rep.FirstOrDefault<MarketPrices>(p => p.DescriptionId == items.DescriptionId && p.CreateTime < DateTime.Now);
                        if (yesterdayPrint == null)
                        {
                            items.Gain = 0;
                            continue;
                        }
                        if (yesterdayPrint.Price > items.Price)
                            items.Gain = 2;
                        else if (yesterdayPrint.Price < items.Price)
                            items.Gain = 1;
                        else
                            items.Gain = 0;
                    }
                }

                return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = modelsM } };
            }

            var sql = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
            var areaAll = _rep.SqlQuery<Area>(sql, string.IsNullOrEmpty(areaName) ? "上海市" : areaName).Select(p => new Area { YunCode = p.YunCode }).ToList();

            List<string> arrt = new List<string>();
            foreach (var item in areaAll)
                arrt.Add(item.YunCode);
            string[] arrt2 = arrt.ToArray();

            var PlaceOfOriginPriceAll = from a in _rep.Where<PlaceOfOriginPrice>(p => p.Status == 0)
                                        where a.CreateTime >= DateTime.Now.AddDays(-30)
                                        join m in _rep.Where<PlaceOfOriginUnit>(p => p.Status == 0) on a.PlaceOfOriginId equals m.Id
                                        join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                        where string.IsNullOrEmpty(areaName) && string.IsNullOrEmpty(areaId) ? a.Status == 0 : !string.IsNullOrEmpty(areaId) ? r.YunCode == areaId : arrt2.Contains(r.YunCode)
                                        // join y in _rep.Where<YscUser>(p => p.Status == 0) on a.UserId equals y.Id
                                        join f in _rep.Where<FishSpecies>(p => p.Status == 0) on a.FishId equals f.FishCode
                                        join d in _rep.Where<Dictionary>(p => p.Status == 0) on f.Type equals d.Id
                                        select new
                                        {
                                            a.Id,
                                            f.FishName,
                                            fishType = d.Name,
                                            // y.UserName,
                                            a.UserId,
                                            m.Address,
                                            a.AveragePrice,
                                            a.CreateTime
                                        };


            var model = PlaceOfOriginPriceAll.OrderByDescending(p => p.CreateTime)
                    .ToPaginationList(index, size)
                    .ToList().Join(_rep.Where<YscUser>(p => p.Status == 0), a => a.UserId, y => y.Id, (a, y) => new { a.Id, a.FishName, y.UserName, a.Address, a.AveragePrice, a.CreateTime, a.UserId }).ToList();


            List<string> arrtIds = new List<string>();
            foreach (var item in model)
                arrtIds.Add(item.Id);
            string[] arrtId = arrtIds.ToArray();

            var marketPricesAll = from a in _rep.Where<PlaceOfOriginPrices>(p => arrtId.Contains(p.PlaceOfOriginPriceId))
                                  join f in _rep.Where<FishSpecies>(s => s.Status == 0) on a.DescriptionId.Trim() equals f.FishCode
                                  select new MarketPricesModel
                                  {
                                      DescriptionId = a.DescriptionId,
                                      MarketPriceId = a.PlaceOfOriginPriceId,
                                      Description = f.Description.Trim(),
                                      Price = a.Price
                                  };

            var marketPricessql = marketPricesAll.ToSql();

            var marketPricesList = marketPricesAll.ToList();



            var models = model
            .Select(p => new MarketPriceModel
            {
                Id = p.Id,
                FishName = p.FishName,
                UserId = p.UserId,
                UserName = p.UserName,
                AveragePrice = p.AveragePrice,
                UnitAddress = p.Address,
                CreateTime = p.CreateTime,
                MarketPricesModel = marketPricesList.Where(a => a.MarketPriceId == p.Id).DistinctBy(a => new { Description = a.Description.Trim() }).ToList()
            }).ToList();


            foreach (var item in models)
            {
                foreach (var items in item.MarketPricesModel)
                {
                    var yesterdayPrint = _rep.FirstOrDefault<PlaceOfOriginPrices>(p => p.DescriptionId == items.DescriptionId && p.CreateTime < DateTime.Now);
                    if (yesterdayPrint == null)
                    {
                        items.Gain = 0;
                        continue;
                    }
                    if (yesterdayPrint.Price > items.Price)
                        items.Gain = 2;
                    else if (yesterdayPrint.Price < items.Price)
                        items.Gain = 1;
                    else
                        items.Gain = 0;
                }
            }

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = models } };

        }

        /// <summary>
        /// 塘口渔家列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="areaName"></param>
        /// <param name="areaId"></param>
        /// <returns></returns>
        public ReturnValueModel GetPlaceOfOriginTimeAll(int index, int size, string areaName, string areaId)
        {
            var fingerAll = _rep.Where<FishSpecies>(p => p.Status == 0).ToList();

            var sql = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE Areaname={0} UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
            var areaAll = _rep.SqlQuery<Area>(sql, string.IsNullOrEmpty(areaName) ? "上海市" : areaName).Select(p => new Area { YunCode = p.YunCode }).ToList();

            List<string> arrt = new List<string>();
            foreach (var item in areaAll)
                arrt.Add(item.YunCode);
            string[] arrt2 = arrt.ToArray();

            var PlaceOfOriginPriceAll = from a in _rep.Where<PlaceOfOriginPrice>(p => p.Status == 0)
                                            // where a.CreateTime == DateTime.Now   
                                        join m in _rep.Where<PlaceOfOriginUnit>(p => p.Status == 0) on a.PlaceOfOriginId equals m.Id
                                        join r in _rep.Where<Area>(p => p.Status == 0) on m.AreaUnCode equals r.YunCode
                                        where string.IsNullOrEmpty(areaName) && string.IsNullOrEmpty(areaId) ? a.Status == 0 : !string.IsNullOrEmpty(areaId) ? r.YunCode == areaId : arrt2.Contains(r.YunCode)
                                        // join y in _rep.Where<YscUser>(p => p.Status == 0) on a.UserId equals y.Id
                                        join f in _rep.Where<FishSpecies>(p => p.Status == 0) on a.FishId equals f.FishCode
                                        join d in _rep.Where<Dictionary>(p => p.Status == 0) on f.Type equals d.Id
                                        select new
                                        {
                                            a.Id,
                                            f.FishName,
                                            fishType = d.Name,
                                            // y.UserName,
                                            a.UserId,
                                            m.Address,
                                            a.AveragePrice,
                                            a.CreateTime
                                        };


            var model = PlaceOfOriginPriceAll.OrderByDescending(p => p.CreateTime)
                    .ToPaginationList(index, size)
                    .ToList().Join(_rep.Where<YscUser>(p => p.Status == 0), a => a.UserId, y => y.Id, (a, y) => new { a.Id, a.FishName, y.UserName, a.Address, a.AveragePrice, a.CreateTime, a.UserId }).ToList();


            List<string> arrtIds = new List<string>();
            foreach (var item in model)
                arrtIds.Add(item.Id);
            string[] arrtId = arrtIds.ToArray();

            var marketPricesAll = from a in _rep.Where<PlaceOfOriginPrices>(p => arrtId.Contains(p.PlaceOfOriginPriceId))
                                  join f in _rep.Where<FishSpecies>(s => s.Status == 0) on a.DescriptionId.Trim() equals f.FishCode
                                  select new MarketPricesModel
                                  {
                                      DescriptionId = a.DescriptionId,
                                      MarketPriceId = a.PlaceOfOriginPriceId,
                                      Description = f.Description.Trim(),
                                      Price = a.Price
                                  };

            var marketPricessql = marketPricesAll.ToSql();

            var marketPricesList = marketPricesAll.DistinctBy(a => new { Description = a.Description.Trim() }).ToList();



            var models = model
            .Select(p => new MarketPriceModel
            {
                Id = p.Id,
                FishName = p.FishName,
                UserId = p.UserId,
                UserName = p.UserName,
                AveragePrice = p.AveragePrice,
                UnitAddress = p.Address,
                CreateTime = p.CreateTime,
                MarketPricesModel = marketPricesList.Where(a => a.MarketPriceId == p.Id).ToList()
            }).ToList();


            foreach (var item in models)
            {
                foreach (var items in item.MarketPricesModel)
                {
                    var yesterdayPrint = _rep.FirstOrDefault<PlaceOfOriginPrices>(p => p.DescriptionId == items.DescriptionId && p.CreateTime < DateTime.Now);
                    if (yesterdayPrint == null)
                    {
                        items.Gain = 0;
                        continue;
                    }
                    if (yesterdayPrint.Price > items.Price)
                        items.Gain = 2;
                    else if (yesterdayPrint.Price < items.Price)
                        items.Gain = 1;
                    else
                        items.Gain = 0;
                }
            }

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = models } };
        }

    }

}
